$(function(){

    //处理首页高度问题
    var screenWorkHeight = $(document).height();//工作区域宽度
    var screenHeight = window.screen.height;
    var topHeight = screenHeight-screenWorkHeight;

    var contentHeight = screenHeight-topHeight-78-50-10;

    $('#left-content').css('height',contentHeight+'px');
    $('#right-content').css('height',contentHeight+'px');
    $('#main').css('height',contentHeight+'px');

    setUserInfo();
    //获取用户信息存页面
    var username= antUtil.getSes('name');
    $('#userName').html(username);

    var regionNm = antUtil.getSes('regionName');
    $('#regionName').html(regionNm);

    //获取一级菜单
    getInitMenu();
    //首页
    getLayout();
    //获取频道信息
    getPagePortlet();

    createTree('homePage');


   $('#changePw').click(function(){
       $('#iframeModal').attr('src','changePw');
       $('#myModal .modal-header .modal-title').html('修改密码');
       $('#myModal').modal();

   })

});


//获取并保存用户信息
function setUserInfo(){

    var userId = antUtil.getSes('userId');

    $.ajax({
        url : "init/getInitUserInfo",
        type : 'GET',
        dataType : "json",
        async:false,
        data : {
            userId:userId
        },
        success : function(data) {
            // todo
            console.log(data);
            antUtil.setSes('name',data[0].name);
            antUtil.setSes('userId',data[0].id.toString());
            antUtil.setSes('appId','HOME_PAGE_HOME_PAGE');

            antUtil.setSes('regionCode',data[0].region.code);
            antUtil.setSes('regionName',data[0].region.name);

        }
    })

}

//获取首页布局
function getLayout(){
    $.ajax({
        url : "layout/getLayoutHtml",
        type : 'GET',
        dataType : "json",
        async:false,
        data : {
            pageId:'HOME_PAGE_YSGL'
        },
        success : function(data) {
            var html = data.html;
            $('#layoutHtml').html(html);
            //一键搜索
            $('#pic1 ').click(function(){
                var searchKey = $('#searchKey').val();
                searchKey = antUtil.base64.encode(searchKey);
                window.open('keySearch?searchKey='+searchKey);
            });

            //一键搜索
            $('#searchKey ').keypress(function(){
                if(event.keyCode==13) {$('#pic1').click();return false;}
            });


            //待办事项更多

            $('#toDoListmore').click(function(){
                var layoutId = $(this).parent().parent().attr('id');
                var url = $(this).parent().parent().attr('data-interface');
                $('#iframeModal').attr('data-layoutId',layoutId);
                $('#iframeModal').attr('data-interface',url);
                $('#iframeModal').attr('src','todoWorkList');
                $('#myModal .modal-header .modal-title').html('待办事项');
                $('#myModal').modal();
            });

            //消息更多
            $('#mesRemain-more').click(function(){
                var layoutId = $(this).parent().parent().attr('id');
                var url = $(this).parent().parent().attr('data-interface');
                $('#iframeModal').attr('data-layoutId',layoutId);
                $('#iframeModal').attr('data-interface',url);
                $('#iframeModal').attr('src','messageList');

                $('#myModal .modal-header .modal-title').html('预警信息');

                $('#myModal').modal();
            });

            //文章更多
            $('#notice-more').click(function(){

                //频道layoutID
                var layoutId = $(this).parent().parent().attr('id');
                var url = $(this).parent().parent().attr('data-interface');
                $('#iframeModal').attr('data-layoutId',layoutId);
                $('#iframeModal').attr('data-interface',url);
                $('#iframeModal').attr('src','articleList');
                $('#myModal .modal-header .modal-title').html('通知公告');
                $('#myModal').modal();
            });


            //文章详细
            $('.notice ul li ').click(function(){
                window.open('articleDetail');
            });

        }
    })
}

//获取频道信息

function getPagePortlet(){
    $.ajax({
        url : "layout/getPagePortletByPageId",
        type : 'GET',
        dataType : "json",
        async:false,
        data : {
            pageId:'HOME_PAGE_YSGL'
        },
        success : function(data) {
            for(x in data){

                var portletId = data[x].portletId;
                var layoutId = data[x].layoutId;

                var _this = $('#'+layoutId);
                //接口
                var url = _this.attr('data-interface');
                //每页显示数目
                var pageLimit = _this.attr('data-limit');
                callFunc(portletId,layoutId,url,pageLimit);

            }
        }
    })
}
//根据函数名得到函数类型
function callFunc(functionName){
    try {
        var  func=eval(functionName);
        //创建函数对象，并调用
        new func(arguments[1],arguments[2],arguments[3]);
    } catch(e) {
            // console.log(functionName+'方法不存在！');
        }

}
